#!/bin/sh
# Compile:by-lanse	2018-12-07

route_vlan=`/sbin/ifconfig br0 |grep "inet addr"| cut -f 2 -d ":"|cut -f 1 -d " " `
username=`nvram get http_username`
CRON_FILE=/etc/storage/cron/crontabs/$username
clear
echo -e "--------------------------------------------------------------------------------"
echo -e "\e[1;31m      全自动安装脚本--\e[1;36m自动运行脚本\e[1;31m包含去AD规则\e[0m"
echo -e "--------------------------------------------------------------------------------\n"

echo -e "\e[1;36m >	1. 安装 \e[1;362mpadavan \033[41;37m纯净Hosts去广告版\e[0m\n"

echo -e "\e[1;36m >	2. 更新 \e[1;36mpadavan 纯净去广告 AD 版本\e[0m\n"

echo -e "\e[1;31m >	3. 卸载 \e[1;36mpadavan 纯净去广告 AD 版本\e[0m\n"

echo -e "\e[1;31m >	0. \033[41;37m退出安装\e[0m\n"

echo -e -n "\e[44;37m 请输入数字继续执行: \e[0m" 
read Function_options

if [ "$Function_options" == "1" ]; then
	echo
	[ -f /tmp/hsfq_script.sh ] && rm -rf /tmp/hsfq_script.sh
	wget --no-check-certificate https://gitee.com/sobweb/Hsfq/raw/master/ss_ad.sh -O \
	/tmp/hsfq_ssad.sh && chmod +x /tmp/hsfq_ssad.sh && . /tmp/hsfq_ssad.sh
	sh /tmp/Hsfq_install
fi

if [ "$Function_options" == "2" ]; then
	sh /etc/storage/bin/hsfq_ssad.sh
	echo -e "\e[1;34m 纯净去广告更新已完成,返回主菜单 \e[0m\n" && sleep 3
	sh /tmp/Hsfq_install
fi

if [ "$Function_options" == "3" ]; then
	if [ -f "/etc/storage/dnsmasq/dnsmasq.conf" ]; then
		echo -e "\e[1;31m 开始卸载启动规则 \e[0m\n"
		sed -i '/addn-hosts/d' /etc/storage/dnsmasq/dnsmasq.conf
	fi

	if [ -f "/etc/storage/cron/crontabs/$username" ]; then
		echo -e "\e[1;31m 删除更新计划任务 \e[0m\n" && sleep 2
		sed -i '/hsfq_ssad/d' /etc/storage/cron/crontabs/$username
		killall crond;/usr/sbin/crond
	fi

	if [ -d "/etc/storage/bin/hosts" ]; then
		echo -e "\e[1;31m 删除残留文件夹以及配置 \e[0m\n" && sleep 2
		rm -rf /etc/storage/bin/hosts
		rm -rf /etc/storage/bin/hsfq_ssad.sh
	fi

	if [ -f "/usr/sbin/dnsmasq" ]; then
		echo -e "\e[1;31m 重启 dnsmasq \e[0m\n"
		restart_dhcpd && /usr/sbin/dnsmasq restart >/dev/null 2>&1
	fi

	[ -f "/tmp/hsfq_ssad.sh" ] && rm -f /tmp/hsfq_ssad.sh
	echo -e "\e[1;34m--纯净去广告卸载完毕,返回主菜单--\e[0m\n" && sleep 3
	sh /tmp/Hsfq_install
fi

if [ "$Function_options" == "0" ]; then
	[ -f /tmp/Hsfq_install ] && rm -f /tmp/Hsfq_install
	[ -f /tmp/hsfq_script.sh ] && rm -f /tmp/hsfq_script.sh
fi
